package com.ywb.ex08;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectDB01 {
    public static void main(String[] args) {
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost: 3306/db_wnb_ex08?useUnicode=true&serverTimezone=GMT%2B8&useSSL=false";
            String username = "root";
            String password = "root";
            Connection conn = DriverManager.getConnection(url, username, password);
            String sql = "insert into tb_users (username,password,sex,age) values('李四','111','男','22')"; // 生成一条sql语句
            Statement stmt = conn.createStatement(); // 创建一个Statment对象
            int i = stmt.executeUpdate(sql);// 执行sql语句
            if(i > 0){
                System.out.println("添加成功");
            }
            stmt.close();
            conn.close(); // 关闭数据库连接对象
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
